package blo;

import java.util.ArrayList;
import java.util.List;

import core.Dictionary;
import core.Word;
import dao.DictionariesDAO;

public class DictionariesBLO {
	private List<Dictionary> listDict = new ArrayList<Dictionary>();
	private DictionariesDAO dictDAO = new DictionariesDAO(); 
	public String lookupWord(int dictID, String word) {
		Dictionary dict = dictDAO.getDictionary(dictID);
		if(dict.existWord(word)) {
			return dict.lookupWord(word);
		}
		else return "not found ";
	}
	public String LookupWord(int fromID, int toID, String word) {
		Dictionary dict = dictDAO.getDictionary(fromID, toID);
		if(dict.existWord(word)) {
			return dict.lookupWord(word);
		}
		else return "not found ";
	}
	public List<Word> getNNearestWords(int dictID, String word){
		Dictionary dict = dictDAO.getDictionary(dictID);
		if(dict.existWord(word)) {
			return dict.getNNearestWords(word);
		}
		else return null;
	}
	public List<Word> getNNearestWords(int fromID, int toID, String word){
		Dictionary dict = dictDAO.getDictionary(fromID, toID);
		if(dict.existWord(word)) {
			return dict.getNNearestWords(word);
		}
		else return null;
	}
    public String getDictListHTML() {
    	listDict = dictDAO.getDictList();
        String distMode = "<table><tr><td style=\"width:70px\">Dictionary: </td><td>";
        distMode += "<select name=slDict style=\"width: 150px\">";
        for (int i = 0; i < listDict.size(); i++) {
            Dictionary dict = listDict.get(i);
            distMode += "<option value=" + dict.getDictID() + ">" + dict.getIfoFile().getStrBookname() + "</option>";
        }
        distMode += "</select>";
        distMode += "</td></tr>";
        distMode += "</table>";
        return distMode;
    }
    public int getDictionaryID(int from, int to) {
    	Dictionary dict = dictDAO.getDictionary(from, to);
    	return dict.getDictID();
    }
    public static void main (String []args) {
    	DictionariesBLO dictBLO = new DictionariesBLO();
    	String word ="fsssscc";
    	List<Word>  listword = new ArrayList<Word>();
    	listword = dictBLO.getNNearestWords(1, word);
    	if(listword!= null) {
    		for(int i = 0; i< listword.size() ; ++i){
    			System.out.println(listword.get(i).strWord);
    		}
    	}
    	System.out.println(dictBLO.lookupWord(1, word));
    }
}